package com.sl.au.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.M2R;
import com.sl.au.entity.Module;
import com.sl.au.entity.Role;
import com.sl.au.service.M2RService;
import com.sl.au.service.ModuleService;
import com.sl.au.service.OptionService;
import com.sl.au.service.RoleService;

@RestController
@RequestMapping(value = "/ui/m2r")
public class M2RController extends BaseController {

	@Autowired
	ModuleService moduleService;
	
	@Autowired
	OptionService optionService;
	
	@Autowired
	RoleService roleService;
	@Autowired
	M2RService m2RService;

	@RequestMapping(value = "new", method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = createLayoutView("m2r/m2rindex");
		return modelAndView;
	}

	@RequestMapping(value = "moduleSet", method = RequestMethod.GET)
	public ModelAndView index(HttpServletRequest request) {
		ModelAndView modelAndView = createLayoutView("m2r/m2rSet");
		String id = request.getParameter("roleId");
		Role role = roleService.getRepository().findOne(id);
		modelAndView.addObject("role", role);
		String dataFilter="数据过滤";
		modelAndView.addObject("dataFilters", optionService.getRepository().findByGroup(dataFilter));
		return modelAndView;

	}
	@RequestMapping(value = "save", method = RequestMethod.GET)
	public Map<String, Object> newAccount(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String roleId = request.getParameter("roleId");
		String moduleId = request.getParameter("moduleId");
		Role role = roleService.getRepository().findOne(roleId);
		Module module = moduleService.getRepository().findOne(moduleId);
		M2R m2r = new M2R();
		m2r.setRole(role);
		m2r.setModule(module);
		m2RService.getRepository().save(m2r);
		map.put("code", 0);
		return map;
	}

	@RequestMapping(value = "getpage", method = RequestMethod.GET)
	public Map<String, Object> getPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String roleId = request.getParameter("roleId");
		Role role = roleService.getRepository().findOne(roleId);
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		List<M2R> m2Rs = m2RService.getModules(start, length, role, null, null);
		int count = m2RService.getRepository().getListNum(role);
		map.put("data", m2Rs);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}

	// 显示所有菜单的tree结构
	@RequestMapping(value = "/getAllNodes", method = RequestMethod.GET)
	public Map<String, Object> getAllNodes(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String menuId = request.getParameter("menuId");
		List<Module> node = null;
		node = moduleService.getAllNodes(menuId);
		List<Map<String, Object>> nodeMap = new ArrayList<Map<String, Object>>();
		nodeMap = moduleService.getNodeMap(node);
		map.put("code", 1);
		map.put("nodes", nodeMap);

		return map;

	}

	// 显示role绑定菜单的tree
	@RequestMapping(value = "/getRoleNodes", method = RequestMethod.GET)
	public Map<String, Object> getRoleNodes(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String roleId = request.getParameter("roleId");
		// 找到角色
		Role role = roleService.getRepository().findOne(roleId);
		// 找到角色配置的模块的个数
		int count = m2RService.getRepository().getListNum(role);
		// 找到角色绑定的m2Rs
		List<M2R> m2Rs = new ArrayList<M2R>();
		if (count > 0) {
			m2Rs = m2RService.getModules(0, count, role, null, null);
		}
		List<Module> modules = new ArrayList<Module>();
		if (m2Rs.size() > 0) {
			for (M2R m2r : m2Rs) {
				Module m = new Module();
				if (m2r != null) {
					m = m2r.getModule();
					modules.add(m);
				}
			}
		}
		List<Map<String, Object>> nodeMap = new ArrayList<Map<String, Object>>();
		nodeMap = moduleService.getNodeMap(modules);
		map.put("nodes", nodeMap);
		map.put("code", 1);
		return map;
	}

	/// 得到要编辑操作权限的菜单模块
	@RequestMapping(value = "/getOperateModal", method = RequestMethod.GET)
	public Map<String, Object> getOperateModal(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String roleId = request.getParameter("roleId");
		// 找到角色
		Role role = roleService.getRepository().findOne(roleId);
		String modalId = request.getParameter("modalId");
		Module module = moduleService.getRepository().findOne(modalId);
		M2R m2r = m2RService.getRepository().findByModuleAndRole(module, role);
		
		String dataFilter=m2r.getDataFilter();
		String read = "";
		if (m2r.isReadOnly()) {
			read = "true";
		} else {
			read = "false";
		}
		map.put("read", read);
		String add = "";
		if (m2r.isAddOnly()) {
			add = "true";
		} else {
			add = "false";
		}
		map.put("add", add);
		String del = "";
		if (m2r.isDeleteOnly()) {
			del = "true";
		} else {
			del = "false";
		}
		map.put("del", del);
		String other1="";
		if(m2r.isOther1Only()){
			other1="true";
		}else{
			other1="false";
		}
		map.put("other1", other1);
		String other2="";
		if(m2r.isOther2Only()){
			other2="true";
		}else{
			other2="false";
		}
		map.put("other2", other2);
		String modify = "";
		if (m2r.isModifyOnly()) {
			modify = "true";
		} else {
			modify = "false";
		}
		map.put("modify", modify);
		map.put("dataFilter", dataFilter);
		
		return map;
	}

	// 删除角色绑定的一个菜单
	@RequestMapping(value = "/deleteRoleNode", method = RequestMethod.GET)
	public Map<String, Object> deleteRoleNode(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String str = request.getParameter("str");
		String ids[] = str.split(",");
		String roleId = request.getParameter("roleId");
		// 找到角色
		Role role = roleService.getRepository().findOne(roleId);
		// 找到要role删除的模块
		for (int i = 0; i < ids.length; i++) {
			String id = ids[i];
			Module module = moduleService.getRepository().findOne(id);
			M2R m2r = m2RService.getRepository().findByModuleAndRole(module, role);
			m2RService.getRepository().delete(m2r);
		}
		map.put("code", 1);
		return map;
	}

	// 保存模块的操作权限
	@RequestMapping(value = "/setModalOpert", method = RequestMethod.POST)
	public Map<String, Object> setOperate(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String roleId = request.getParameter("roleId");
		String readonly = request.getParameter("readonly");
		String delonly = request.getParameter("delonly");
		String addonly = request.getParameter("addonly");
		String modifyonly = request.getParameter("modifyonly");
		String other1only=request.getParameter("other1only");
		String other2only=request.getParameter("other2only");
		String dataFilter= request.getParameter("dataFilter");
		Role role = roleService.getRepository().findOne(roleId);
		String modalId = request.getParameter("modalId");
		Module module = moduleService.getRepository().findOne(modalId);
		M2R m2r = m2RService.getRepository().findByModuleAndRole(module, role);
		m2r.setDataFilter(dataFilter);
		if(readonly.equals("true")){
			m2r.setReadOnly(true);
		}else{
			m2r.setReadOnly(false);
		}
		if(delonly.equals("true")){
			m2r.setDeleteOnly(true);
		}else{
			m2r.setDeleteOnly(false);
		}
		if(addonly.equals("true")){
			m2r.setAddOnly(true);
		}else{
			m2r.setAddOnly(false);
		}
		if(other1only.equals("true")){
			m2r.setOther1Only(true);
		}else{
			m2r.setOther1Only(false);
		}
		if(other2only.equals("true")){
			m2r.setOther2Only(true);
		}else{
			m2r.setOther2Only(false);
		}
		if(modifyonly.equals("true")){
			m2r.setModifyOnly(true);
		}else{
			m2r.setModifyOnly(false);
		}
		m2RService.getRepository().save(m2r);
		return map;
	}

	// 得到树状图选的节点，在m2r表中重新配置
	@RequestMapping(value = "/setNodes", method = RequestMethod.GET)
	public Map<String, Object> setNodes(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String removeNodesId[] = request.getParameterValues("removeNodesId[]");
		String addNodesId[] = request.getParameterValues("addNodesId[]");
		String roleId = request.getParameter("roleId");
		// 找到角色
		Role role = roleService.getRepository().findOne(roleId);
		// 找到角色配置的要删除的模块
		List<M2R> removeM2rs=new ArrayList<M2R>();
		if(removeNodesId!=null){
			for(int i=0;i<removeNodesId.length;i++){
				if(removeNodesId[i]!=null){
					String id = removeNodesId[i];
					Module module = moduleService.getRepository().findOne(id);
					M2R m2r=m2RService.getRepository().findByModuleAndRole(module, role);
					removeM2rs.add(m2r);
				}	
			}
			if(removeM2rs.size()>0){
				m2RService.getRepository().deleteInBatch(removeM2rs);
			}
		}
		
		
		
		// 在配置新添加的模块
		List<M2R> addM2rs=new ArrayList<M2R>();
		if(addNodesId!=null){
			for (int i = 0; i < addNodesId.length; i++) {
				if (addNodesId[i] != null) {
					String id = addNodesId[i];
					Module module = moduleService.getRepository().findOne(id);
					M2R m2r = new M2R();
					m2r.setRole(role);
					m2r.setModule(module);
					m2r.setAddOnly(true);
					m2r.setDeleteOnly(true);
					m2r.setReadOnly(true);
					m2r.setOther1Only(true);
					m2r.setOther2Only(true);
					m2r.setModifyOnly(true);
					m2r.setDataFilter("所有");
					m2RService.getRepository().save(m2r);
					addM2rs.add(m2r);
				}
			}
			if(addM2rs.size()>0){
				m2RService.getRepository().save(addM2rs);
			}
		}
		
		
		
		///获得role的所有模块
		List<Module> roleNodes = new ArrayList<Module>();
		roleNodes=m2RService.getRepository().findModuleByRole(role);
		// 将重新配置的菜单树，返回显示
		List<Map<String, Object>> nodeMap = new ArrayList<Map<String, Object>>();
		nodeMap = moduleService.getNodeMap(roleNodes);
		map.put("nodes", nodeMap);
		map.put("code", 1);
		return map;
	}
}
